//
// Created by yonghu0 on 25-10-7.
//
#include <iostream>
#include <vector>
#include <algorithm> //copy的头文件
#include <iterator> //ostream_iterator头文件

int main()
{
    std::cout << "vector_test begin" << std::endl;

    std::cout << "------创建vector------" << std::endl;
    std::vector<int> v1;
    std::vector<int> v2(3, 0); //创建vector，size为3，值都为0

    for (int i = 0; i < v1.size(); i++)
    {
        std::cout << v1[i] << " ";
    }
    std::cout << std::endl;
    for (int i = 0; i < v2.size(); i++)
    {
        std::cout << v2[i] << " ";
    }
    std::cout << std::endl;

    std::cout << "-------获取size------" << std::endl;
    std::cout << v1.size() << std::endl;
    std::cout << "-------判断vector是否为空------" << std::endl;
    std::cout << v1.empty() << std::endl;

    std::cout << "--------------增--------------" << std::endl;
    std::cout << "--尾部添加元素--" << std::endl;
    v1.push_back(3);
    v1.push_back(2);
    v1.push_back(1);
    v1.push_back(4);
    std::ostream_iterator<int> oit(std::cout, " ");
    std::copy(v1.begin(), v1.end(), oit);
    std::cout << std::endl;
    std::cout << "--插入元素--" << std::endl;
    //插入单个元素
    std::vector<int>::iterator it1 = std::find(v1.begin(), v1.end(), 2);
    std::vector<int>::iterator it2 = v1.insert(it1, 99);
    std::copy(v1.begin(), v1.end(), oit);
    std::cout << std::endl;
    std::cout << *it2 << std::endl;
    //插入区间
    int a1[] = {11, 12, 13, 14};
    std::vector<int>::iterator it3 = v1.insert(it2, a1, a1 + 4);
    std::copy(v1.begin(), v1.end(), oit);
    std::cout << std::endl;
    std::cout << *it3 << std::endl;

    std::cout << "-------------查找---------------" << std::endl;
    if (std::find(v1.begin(), v1.end(), 2) != v1.end())
    {
        std::cout << "容器中存在元素" << std::endl;
    } else
    {
        std::cout << "容器中不存在元素" << std::endl;
    }

    std::cout << "-------------删除---------------" << std::endl;
    std::cout << "----尾部删除元素-----" << std::endl;
    v1.pop_back();
    std::copy(v1.begin(), v1.end(), oit);
    std::cout << std::endl;
    std::cout << "----删除元素-----" << std::endl;
    //删除单个元素
    std::vector<int>::iterator it4 = v1.erase(v1.begin());
    std::copy(v1.begin(), v1.end(), oit);
    std::cout << std::endl;
    std::cout << *it4 << std::endl;
    //删除区间
    std::vector<int>::iterator it5 = v1.erase(v1.begin(), v1.begin() + 2);
    std::copy(v1.begin(), v1.end(), oit);
    std::cout << std::endl;
    std::cout << *it5 << std::endl;

    std::cout << "-----------------------遍历---------------------" << std::endl;
    std::cout << "---正向遍历---" << std::endl;
    //方法一
    for (auto elem: v1)
    {
        std::cout << elem << " ";
    }
    std::cout << std::endl;
    //方法二
    for (size_t i = 0; i < v1.size(); i++)
    {
        std::cout << v1[i] << " ";
    }
    std::cout << std::endl;
    //方法三
    for (std::vector<int>::iterator it = v1.begin(); it != v1.end(); it++)
    {
        std::cout << *it << " ";
    }
    std::cout << std::endl;
    std::cout << "---反向遍历---" << std::endl;
    //方法一
    for (size_t i = 0; i < v1.size(); i++)
    {
        std::cout << v1[v1.size() - i - 1] << " ";
    }
    std::cout << std::endl;
    //方法二
    for (std::vector<int>::reverse_iterator it = v1.rbegin(); it != v1.rend(); it++)
    {
        std::cout << *it << " ";
    }
    std::cout << std::endl;

    std::cout << "-----------清空容器中的所有元素-------------" << std::endl;
    v1.clear();
    std::copy(v1.begin(), v1.end(), oit);
    std::cout << std::endl;

    std::cout << "vector_test end" << std::endl;
    return 0;
}
